package edu.xmut.kakamusic.fragment.video;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.util.Objects;

import edu.xmut.kakamusic.R;
import edu.xmut.kakamusic.activity.tiktok.TiktokActivity;
import edu.xmut.kakamusic.adapter.video.VideoAdapter;
import edu.xmut.kakamusic.bean.entity.Video;
import edu.xmut.kakamusic.utils.ToastUtils;
import edu.xmut.kakamusic.utils.WindowUtils;
import edu.xmut.kakamusic.viewmodel.VideoViewModel;


public class VideoFragment extends Fragment {


    private VideoViewModel videoViewModel;
    private VideoAdapter videoAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        videoViewModel = new ViewModelProvider(this).get(VideoViewModel.class);
        View root = inflater.inflate(R.layout.fragment_video, container, false);
        RefreshLayout refreshLayout = root.findViewById(R.id.refreshLayout_video);
        refreshLayout.setOnRefreshListener(layout -> {
            layout.finishRefresh(2000);
            getVideoList();
        });
        refreshLayout.setOnLoadMoreListener(layout -> {
            layout.finishLoadMore(2000);
            ToastUtils.show(requireContext(), "没有更多数据了...");
        });
        // 获取状态栏高度
        int statusBarHeight = WindowUtils.getStatusBarHeight(requireContext());
        RecyclerView recyclerView = root.findViewById(R.id.recyclerview_video);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(0, statusBarHeight + 30, 0, 0);
        recyclerView.setLayoutParams(params);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        videoAdapter = new VideoAdapter(null);
        recyclerView.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener((adapter, view, position) -> {
            // 如果点击的第一个
            if (Objects.equals(position, 0)) {
                startActivity(new Intent(requireContext(), TiktokActivity.class));
                return;
            }
            Video video = videoAdapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putString("image", video.getImg());
            bundle.putString("name", video.getName());
            bundle.putString("intro", video.getIntro());
            bundle.putStringArray("list", video.getVideoDetailList().stream().map(Video.VideoDetailList::getVideoName).toArray(String[]::new));
            Navigation.findNavController(root)
                    .navigate(R.id.action_menu_video_to_videoDetailFragment, bundle);
        });
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        getVideoList();
    }

    private void getVideoList() {
        videoViewModel.getVideoList().observe(getViewLifecycleOwner(), videos -> {
            videoAdapter.setList(videos);
        });
    }
}